#ifndef SYMBOLIC_H
#define SYMBOLIC_H
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <iostream>


class symbolic
{
    char* varSym,* operators;
    int qVarOp;
public:
    symbolic(){}
    symbolic(char variable);
    ~symbolic(){}
    void resizeSyms(int tam);
    symbolic defautOperation(symbolic S1, char temp);
    symbolic operator + (symbolic S1);
    symbolic operator * (symbolic S1);
    void print();
};

symbolic::symbolic(char variable)
{
    this->qVarOp = 0;
    this->varSym = (char*)malloc(sizeof(char));
    this->operators = (char*)malloc(sizeof(char));
    this->varSym[this->qVarOp] = variable;
    this->operators[this->qVarOp] = ' ';
}

void symbolic::resizeSyms(int tam)
{
    this->qVarOp += tam + 1;
    this->varSym = (char*)realloc(this->varSym, this->qVarOp*sizeof(char));
    this->operators = (char*)realloc(this->operators, this->qVarOp*sizeof(char));
}

symbolic symbolic::defautOperation(symbolic S1, char temp)
{
    symbolic ret;
    int cont = 0;
    ret.qVarOp = this->qVarOp + S1.qVarOp + 1;
    ret.varSym = (char*)malloc(ret.qVarOp*sizeof(char));
    ret.operators = (char*)malloc(ret.qVarOp*sizeof(char));

    for(int i = 0; i <= this->qVarOp; i++)
    {
        ret.varSym[i] = this->varSym[i];
        ret.operators[i] = this->operators[i];
        cont++;
    }
    for(int i = 0; i <= S1.qVarOp; i++)
    {
        ret.varSym[cont] = S1.varSym[i];
        ret.operators[cont] = S1.operators[i];
        cont++;
    }

//    ret.resizeSyms(S1.qVarOp);
    ret.operators[this->qVarOp] = temp;

    return ret;
}

symbolic symbolic::operator + (symbolic S1)
{
    symbolic ret = this->defautOperation(S1, '+');
    return ret;
}

symbolic symbolic::operator * (symbolic S1)
{
    bool cont = true;

    for(int i = 0; i <= this->qVarOp; i++)
    {
        if((this->operators[i] == '+') || (this->operators[i] == '-') )
        {
            cont = false;
            break;
        }
    }
    for(int i = 0; i <= S1.qVarOp; i++)
    {
        if((S1.operators[i] == '+') || S1.operators[i] == '-')
        {
            cont = false;
            break;
        }
    }

    if((S1.qVarOp == 0 && this->qVarOp == 0) || cont == true)
    {
        symbolic ret = this->defautOperation(S1, '*');
        return ret;
    }
    return *this;
}

void symbolic::print()
{
    for (int i = 0; i <= this->qVarOp; i++)
    {
        std::cout << this->varSym[i] << ' ' << this->operators[i] << ' ';
    }
    std::endl (std::cout);

}

#endif // SYMBOLIC_H
